﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

using System.Security.Cryptography;

namespace GestorDeFlotasDesktop
{
    static class ValidarHelpers
    {
        public static bool IsNumeric(string strNumero)
        {
            return ValidarHelpers.IsNumeric(strNumero, 0, int.MaxValue);
        }

        public static bool IsNumeric(string strNumero, int intValorMinimo)
        {
            return ValidarHelpers.IsNumeric(strNumero, 0, int.MaxValue);
        }
        public static bool IsNumeric(string strNumero, int intValorMinimo, int intValorMaximo)
        {
            Boolean blnnumeric = true;
            int i2;
            try
            {
                i2 = Convert.ToInt32(strNumero);
                if (i2 < intValorMinimo || i2 > intValorMaximo)
                {
                    blnnumeric = false;
                }
            }
            catch
            {
                blnnumeric = false;

            }


            return blnnumeric;
        }
        public static bool IsDouble(string strNumero)
        {
            return ValidarHelpers.IsDouble(strNumero, 0, double.MaxValue);
        }

        public static bool IsDouble(string strNumero, double dblValorMinimo)
        {
            return ValidarHelpers.IsDouble(strNumero, 0, double.MaxValue);
        }
        public static bool IsDouble(string strNumero, double dblValorMinimo, double dblValorMaximo)
        {
            Boolean blnnumeric = true;
            double i2;
            if (strNumero == "")
            {
                strNumero = "0";
            }
            try
            {
                i2 = Convert.ToDouble(strNumero);
                if (i2 < dblValorMinimo || i2 > dblValorMaximo)
                {
                    blnnumeric = false;
                }
            }
            catch
            {
                blnnumeric = false;

            }


            return blnnumeric;
        }
        public static bool IsTel(string strTel)
        {
            bool blnReturn = false;
            
            bool blnesnumero = false;
            int intErrores = 0;
            int intError = 0;
            int intCantnum = 0;
            bool blnesguion = false;
            int intCantguiones = 0;
            int intCant = strTel.Length;
            string strCaracter;
            if (intCant >= 8)
            {
                for (int i = 0; i < intCant; i++)
                {
                    strCaracter = strTel.Substring(i, 1);
                    blnesnumero = ValidarHelpers.IsNumeric(strCaracter);
                    if (blnesnumero == true)
                    {
                        intErrores = 0;
                        intCantnum = intCantnum + 1;
                    }
                    else
                    {
                        blnesguion = ValidarHelpers.IsStringPresent(strCaracter);
                        if (blnesguion == true)
                        {
                            if (strCaracter == "-" || strCaracter == "/")
                            {
                                intError = 0;
                                intCantguiones = intCantguiones + 1;
                            }
                        }
                    }
                }
                if (intCantnum >= 8 && intError == 0 && intErrores == 0)
                {
                    blnReturn = true;
                }
            }
            return blnReturn;
        }
        public static bool IsCuit(string strCuit)
        {
            bool blnvalido = false;
            bool blnesguion = false;
            int intCantnum = 0;
            int intCantguiones = 0;
            int intErrores = 1;
            int intError = 1;
            bool blnesnumero = false;
            string strCaracter;

            for (int i = 0; i < strCuit.Length; i++)
            {
                strCaracter = strCuit.Substring(i, 1);
                blnesnumero = ValidarHelpers.IsNumeric(strCaracter);
                if (blnesnumero == true)
                {
                    intErrores = 0;
                    intCantnum = intCantnum + 1;
                }
                else
                {
                    blnesguion = ValidarHelpers.IsStringPresent(strCaracter);
                    if (blnesguion == true)
                    {
                        if (strCaracter == "-")
                        {
                            intError = 0;
                            intCantguiones = intCantguiones + 1;
                        }
                    }
                }
            }
            if (intErrores == 0 && intError == 0 && intCantguiones == 2 && intCantnum == 11)
            {
                bool blnReturn;
                blnReturn = false;
                int intVerificacion = 0;
                int intResto = 0;
                int intSuma = 0;

                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(0, 1)) * 5;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(1, 1)) * 4;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(3, 1)) * 3;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(4, 1)) * 2;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(5, 1)) * 7;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(6, 1)) * 6;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(7, 1)) * 5;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(8, 1)) * 4;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(9, 1)) * 3;
                intSuma = intSuma + Convert.ToInt32(strCuit.Substring(10, 1)) * 2;

                intResto = intSuma % 11;
                intVerificacion = 11 - intResto;
                if (intVerificacion == 1)
                {
                    intVerificacion = 9;
                }
                if (intVerificacion == 0)
                {
                    intVerificacion = 0;
                }
                if (intVerificacion == Convert.ToInt32(strCuit.Substring(12, 1)))
                {
                    blnReturn = true;
                }
                else
                {
                    blnReturn = false;
                }
                return blnReturn;
            }
            else
            {
                return blnvalido;
            }
        }
        public static bool IsPrecio(string strPrec)
        {
            bool blnReturn = true;
            bool blnesnum = false;
            int intCantCaracteres = strPrec.Length;
            string strcaracter = "";
            for (int i = 0; i < intCantCaracteres; i++)
            {
                strcaracter = strPrec.Substring(i, 1);
                blnesnum = ValidarHelpers.IsNumeric(strcaracter);
                if (blnesnum == false)
                {
                    if (strcaracter != ".")
                    {
                        blnReturn = false;
                    }
                }
            }
            return blnReturn;
        }
        public static bool IsStringPresent(string strTexto)
        {
            return IsStringPresent(strTexto, 1, int.MaxValue);
        }
        public static bool IsStringPresent(string strTexto, int intLongMinima)
        {
            return IsStringPresent(strTexto, 1, int.MaxValue);
        }
        public static bool IsStringPresent(string strTexto, int intLongMinima, int intLongMaxima)
        {
            bool blnString = true;

            if (strTexto.Length < 1 || strTexto.Length < intLongMinima || strTexto.Length > intLongMaxima)
            {
                blnString = false;
            }

            return blnString;
        }
    }
}
